package com.sugon.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.ScrapAcceRecordDao;
import com.sugon.dao.SpareScrapDetailDao;
import com.sugon.entity.ScrapAcceRecordEntity;
import com.sugon.entity.SpareScrapDetailEntity;
import com.sugon.entity.SysMsgEntity;
import com.sugon.service.SpareScrapDetailService;
import com.sugon.service.SysMsgService;
import com.sugon.service.SysProcessInfoService;
import com.sugon.utils.ProcessInfoEnum;
import com.sugon.utils.ShiroUtils;
import com.sugon.utils.SysMsgType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.sugon.utils.ShiroUtils.getUserId;

/**
 * 报废记录详细Service实现类
 *
 * @author sugon
 * @date 2019-08-01 17:32:16
 */
@Service("spareScrapDetailService")
public class SpareScrapDetailServiceImpl implements SpareScrapDetailService {
    @Autowired
    private SpareScrapDetailDao spareScrapDetailDao;
    @Autowired
    private ScrapAcceRecordDao scrapAcceRecordDao;
    @Autowired
    private SysProcessInfoService sysProcessInfoService;
    @Autowired
    private SysMsgService sysMsgService;

    /**
     * queryObject
     *
     * @param id 主键
     * @return
     */
    @Override
    public SpareScrapDetailEntity queryObject(String id) {
        return spareScrapDetailDao.queryObject(id);
    }

    /**
     * queryList
     *
     * @param map 参数
     * @return
     */
    @Override
    public List<SpareScrapDetailEntity> queryList(Map<String, Object> map) {
        return spareScrapDetailDao.queryList(map);
    }

    /**
     * queryTotal
     *
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return spareScrapDetailDao.queryTotal(map);
    }

    /**
     * save
     *
     * @param spareScrapDetail 实体
     * @return
     */
    @Override
    public int save(SpareScrapDetailEntity spareScrapDetail) {
        spareScrapDetail.setId(RandomUtil.randomString(32));
        spareScrapDetail.setCreateTime(new Date());
        spareScrapDetail.setCreateUser(getUserId());

        spareScrapDetail.setApprovalUser(getUserId());
        //添加审核 消息提醒
        sysProcessInfoService.saveProcessInfo(spareScrapDetail.getId(), ProcessInfoEnum.SPA_BJBF, 0, null);
        SysMsgEntity sysMsgEntity = new SysMsgEntity();
        sysMsgEntity.setDataId(spareScrapDetail.getId());
        sysMsgEntity.setDataUrl("../spare/sparescrapdetail.html?scrapNo=" + spareScrapDetail.getId());
        sysMsgEntity.setType(SysMsgType.APPROVAL_REMIND);
        sysMsgEntity.setUserId(ShiroUtils.getUserId());
        sysMsgEntity.setDept(ShiroUtils.getDeptId());
        sysMsgEntity.setTitle(ProcessInfoEnum.SPA_BJBF.getDesc());
        sysMsgEntity.setDataType(ProcessInfoEnum.SPA_BJBF);
        sysMsgService.allMsgData(sysMsgEntity);
        return spareScrapDetailDao.save(spareScrapDetail);
    }

    @Override
    public int update(SpareScrapDetailEntity spareScrapDetail) {
        spareScrapDetail.setUpdateTime(new Date());
        spareScrapDetail.setUpdateUser(getUserId());
        return spareScrapDetailDao.update(spareScrapDetail);
    }

    @Override
    public int delete(String id) {
        return spareScrapDetailDao.delete(id);
    }

    @Override
    public int deleteBatch(String[] ids) {
        return spareScrapDetailDao.deleteBatch(ids);
    }

    @Override
    public int review(SpareScrapDetailEntity spareScrapDetail) {
        ScrapAcceRecordEntity scrapAcceRecordEntity = scrapAcceRecordDao.queryBycode(spareScrapDetail.getScrapCode());
        scrapAcceRecordEntity.setStatus(1);
        scrapAcceRecordEntity.setCheckDate(new Date());
        scrapAcceRecordEntity.setCheckUser(getUserId());
        scrapAcceRecordDao.update(scrapAcceRecordEntity);
        spareScrapDetail.setApprovalTime(new Date());
        spareScrapDetail.setApprovalUser(getUserId());
        sysProcessInfoService.saveProcessInfo(spareScrapDetail.getId(), ProcessInfoEnum.SPA_RKSP, Integer.valueOf(spareScrapDetail.getApprovalUser()), null);
        SysMsgEntity sysMsgEntity = new SysMsgEntity();
        sysMsgEntity.setDataId(spareScrapDetail.getId());
        sysMsgEntity.setDataType(ProcessInfoEnum.SPA_RKSP);
        sysMsgEntity.setDataFlag(SysMsgType.DATA_DEL_END);
        sysMsgService.allMsgData(sysMsgEntity);
        return spareScrapDetailDao.update(spareScrapDetail);
    }
}
